const getDefaultState = () => {
  return {
    token: localStorage.getItem('token') || '',
    uniqueId: '',
    name: '',
    avatar: '',
    roles: [],
    recommendCode: '',
    loginInfo: {}
  };
};

const state = getDefaultState();

const mutations = {
  // 设置token
  setToken: (state, token) => {
    localStorage.setItem('token', token);
    state.token = token;
  },
  // 设置角色
  setRoles: (state, roles) => {
    state.roles = roles;
  },
  // 退出登录
  signOut: (state, path) => {
    let loginOrigin = process.env.VUE_APP_LOGIN_APP_ORIGIN;

    if (path) {
      let redirect = window.location.pathname;
      let language = localStorage.getItem('language');
      let theme = localStorage.getItem('theme');
      theme = theme.replace('#', '');
      let appOrigin = window.location.origin;
      loginOrigin = `${loginOrigin}?language=${language}&theme=${theme}&appOrigin=${appOrigin}&redirect=${redirect}`;
    }
    // 重置参数
    Object.assign(state, getDefaultState());
    localStorage.removeItem('token');
    window.location.href = loginOrigin;
  }
};

const actions = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
